package com.crm.controller;

import com.crm.pojo.common.PageResult;
import com.crm.pojo.common.Result;
import com.crm.pojo.domain.Contract;
import com.crm.pojo.dto.ContractQueryDto;
import com.crm.pojo.vo.ContractDetailVo;
import com.crm.pojo.vo.ContractVo;
import com.crm.service.ContractService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author: xaw
 * @CreateTime: 2025-09-04  12:23:09
 * @Description: 合同
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/contract")
public class ContractController {
    private final ContractService contractService;

    /**
     * 合同分页查询
     *
     * @param contractQueryDto
     * @return
     */
    /*/list?pageNum=1&pageSize=10&params%5BbeginCreateTime%5D=&params%5BendCreateTime%5D=*/
    @RequestMapping("/list")
    public PageResult list(ContractQueryDto contractQueryDto) {
        return contractService.pageQuery(contractQueryDto);
    }

    /**
     * 获取合同详情
     *
     * @param id
     * @return
     */
    /* http://localhost:8888/api/contract/1 */
    @GetMapping("/{id}")
    public Result getContractDetail(@PathVariable Integer id) {
        ContractDetailVo contractDetail = contractService.getContractDetail(id);
        log.info("获取合同详情：{}", contractDetail);
        return Result.success(contractDetail);
    }

    /**
     * 添加合同
     *
     * @param contract
     * @return
     */
    @RequestMapping
    public Result add(@RequestBody Contract contract) {
        return Result.success(contractService.save(contract));
    }

    /**
     * 修改合同回显
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Result getById(@PathVariable Integer id) {
        Contract contract = contractService.getById(id);
        return Result.success(contract);
    }

    /**
     * 修改合同
     *
     * @param contract
     * @return
     */
    /* http://localhost:8888/api/contract/detail/2 */
    @PutMapping
    public Result updateselect(@RequestBody Contract contract) {
        return Result.success(contractService.updateById(contract));
    }

    /**
     * 删除合同
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(contractService.removeById(id));
    }

    @PutMapping("/changeContract/{id}")
    public Result changeContract(@PathVariable Integer id ,@RequestBody ContractQueryDto dto) {
        log.info("商机转合同");
        try {
            contractService.changeContract(id,dto);
            return Result.success();
        } catch (Exception e) {
            log.info(e.getMessage());
            return Result.fail(e.getMessage());
        }
    }


}
